﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;

namespace PMS.ChucNang
{
    public partial class ChiTietNhapLookup : FormPopupBase
    {
        private readonly ChiTietPhieuNhapBO service = new ChiTietPhieuNhapBO();
        public ChiTietPhieuNhap ChiTietPhieuNhapSelected;
        string MaThuoc = null; 
        int? lonhon;
        string MaKho = null;
        public ChiTietNhapLookup(int? lon, string makho, string mathuoc = null)
        {
            MaKho = makho;
            this.lonhon = lon;
            MaThuoc = mathuoc;
            InitializeComponent();
            InitStyle();
            InitData();
        }

        public ChiTietNhapLookup(string Makho, string mathuoc = null )
        {
            MaKho = Makho;
            MaThuoc = mathuoc;
            InitializeComponent();
            InitStyle();
            InitData();
        }
      
        void InitStyle()
        {
            grvChiTietPhieuNhapList.OptionsBehavior.Editable = false;
            grvChiTietPhieuNhapList.OptionsView.ShowGroupPanel = false;
            grvChiTietPhieuNhapList.OptionsView.ShowIndicator = true;

            grvChiTietPhieuNhapList.OptionsView.ShowAutoFilterRow = true;
            grvChiTietPhieuNhapList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvChiTietPhieuNhapList.OptionsView.ShowGroupedColumns = false;
            grvChiTietPhieuNhapList.OptionsMenu.EnableColumnMenu = false;
            grvChiTietPhieuNhapList.OptionsCustomization.AllowColumnMoving = false;
            grvChiTietPhieuNhapList.OptionsCustomization.AllowGroup = false;
            grvChiTietPhieuNhapList.OptionsFilter.AllowFilterEditor = false;

            this.grvChiTietPhieuNhapList.OptionsSelection.MultiSelect = false;
            this.grvChiTietPhieuNhapList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colMaThuoc.FieldName = LuuKhoColumns.MaThuoc.ToString();
            this.colSoLo.FieldName = LuuKhoColumns.SoLo.ToString();
            this.colSoPN.FieldName = ChiTietPhieuNhapColumns.SoPN.ToString();
            this.colSoLuong.FieldName = ChiTietPhieuNhapColumns.SoLuong.ToString();
            this.colDonGia.FieldName = ChiTietPhieuNhapColumns.DonGia.ToString();
            this.colNgaySX.FieldName = LuuKhoColumns.NgaySX.ToString();
            this.colHanDung.FieldName = LuuKhoColumns.HanDung.ToString();
            this.colHanSD.FieldName = LuuKhoColumns.HanSuDung.ToString();
            

        }

        void InitData()
        {
            try
            {
                if (MaThuoc == null)
                {
                    this.grdChiTietPhieuNhapList.DataSource = service.GetAllChiTietPhieuNhap();
                }
                else
                {
                    this.grdChiTietPhieuNhapList.DataSource = service.GetAllChiTietPhieuNhapByMaThuoc(MaThuoc,MaKho,lonhon);
                }
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.ChiTietPhieuNhapSelected = (ChiTietPhieuNhap)grvChiTietPhieuNhapList.GetFocusedRow();
                }
                else
                {
                    this.ChiTietPhieuNhapSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvChiTietPhieuNhapList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvChiTietPhieuNhapList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdChiTietPhieuNhapList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (ChiTietPhieuNhapSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvChiTietPhieuNhapList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvChiTietPhieuNhapList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            ChiTietPhieuNhapSelected = this.grvChiTietPhieuNhapList.GetFocusedRow() as ChiTietPhieuNhap;
        }

        private void grvChiTietPhieuNhapList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
